home *** CD-ROM | disk | FTP | other *** search
/ System Booster / System Booster.iso / Archives / GNU / GNUPLOTsrc.lha / standard.c < prev    next >
Encoding:
C/C++ Source or Header  |  1996-01-22  |  20.6 KB  |  984 lines

  1. #ifndef lint
  2. static char *RCSid = "$Id: standard.c,v 1.11 1995/06/16 09:31:47 drd Exp $";
  3. #endif
  4.  
  5.  
  6. /* GNUPLOT - standard.c */
  7. /*
  8.  * Copyright (C) 1986 - 1993   Thomas Williams, Colin Kelley
  9.  *
  10.  * Permission to use, copy, and distribute this software and its
  11.  * documentation for any purpose with or without fee is hereby granted, 
  12.  * provided that the above copyright notice appear in all copies and 
  13.  * that both that copyright notice and this permission notice appear 
  14.  * in supporting documentation.
  15.  *
  16.  * Permission to modify the software is granted, but not the right to
  17.  * distribute the modified code.  Modifications are to be distributed 
  18.  * as patches to released version.
  19.  *  
  20.  * This software is provided "as is" without express or implied warranty.
  21.  * 
  22.  *
  23.  * AUTHORS
  24.  * 
  25.  *   Original Software:
  26.  *     Thomas Williams,  Colin Kelley.
  27.  * 
  28.  *   Gnuplot 2.0 additions:
  29.  *       Russell Lang, Dave Kotz, John Campbell.
  30.  *
  31.  *   Gnuplot 3.0 additions:
  32.  *       Gershon Elber and many others.
  33.  * 
  34.  */
  35.  
  36. #include <math.h>
  37. #include "plot.h"
  38. #include "setshow.h" /* for ang2rad */
  39. #include "fnproto.h"
  40.  
  41. extern struct value stack[STACK_DEPTH];
  42. extern int s_p;
  43. extern double zero;
  44.  
  45. static double jzero __P((double x));
  46. static double pzero __P((double x));
  47. static double qzero __P((double x));
  48. static double yzero __P((double x));
  49. static double rj0 __P((double x));
  50. static double ry0 __P((double x));
  51. static double jone __P((double x));
  52. static double pone __P((double x));
  53. static double qone __P((double x));
  54. static double yone __P((double x));
  55. static double rj1 __P((double x));
  56. static double ry1 __P((double x));
  57.  
  58. /* The bessel function approximations here are from
  59.  * "Computer Approximations"
  60.  * by Hart, Cheney et al.
  61.  * John Wiley & Sons, 1968
  62.  */
  63.  
  64. /* There appears to be a mistake in Hart, Cheney et al. on page 149.
  65.  * Where it list Qn(x)/x ~ P(z*z)/Q(z*z), z = 8/x, it should read
  66.  *               Qn(x)/z ~ P(z*z)/Q(z*z), z = 8/x
  67.  * In the functions below, Qn(x) is implementated using the later
  68.  * equation.
  69.  * These bessel functions are accurate to about 1e-13
  70.  */
  71.  
  72. #if (defined (ATARI) && defined(__PUREC__)) || (defined (MTOS) && defined(__PUREC__))
  73. /* Sorry. But PUREC bugs here.
  74.  * These bessel functions are NOT accurate to about 1e-13
  75.  */
  76.  
  77. #define PI_ON_FOUR     0.785398163397448309615661
  78. #define PI_ON_TWO     1.570796326794896619231313
  79. #define THREE_PI_ON_FOUR 2.356194490192344928846982
  80. #define TWO_ON_PI     0.636619772367581343075535
  81.  
  82. static double dzero = 0.0;
  83.  
  84. /* jzero for x in [0,8]
  85.  * Index 5849, 19.22 digits precision
  86.  */
  87. static double pjzero[] = {
  88.      0.493378725179413356181681e+21,
  89.     -0.117915762910761053603844e+21,
  90.      0.638205934107235656228943e+19,
  91.     -0.136762035308817138686542e+18,
  92.      0.143435493914034611166432e+16,
  93.     -0.808522203485379387119947e+13,
  94.      0.250715828553688194555516e+11,
  95.     -0.405041237183313270636066e+8,
  96.      0.268578685698001498141585e+5
  97. };
  98.  
  99. static double qjzero[] = {
  100.      0.493378725179413356211328e+21,
  101.      0.542891838409228516020019e+19,
  102.      0.302463561670946269862733e+17,
  103.      0.112775673967979850705603e+15,
  104.      0.312304311494121317257247e+12,
  105.      0.669998767298223967181403e+9,
  106.      0.111463609846298537818240e+7,
  107.      0.136306365232897060444281e+4,
  108.      0.1e+1
  109. };
  110.  
  111. /* pzero for x in [8,inf]
  112.  * Index 6548, 18.16 digits precision
  113.  */
  114. static double ppzero[] = {
  115.      0.227790901973046843022700e+5,
  116.      0.413453866395807657967802e+5,
  117.      0.211705233808649443219340e+5,
  118.      0.348064864432492703474453e+4,
  119.      0.153762019090083542957717e+3,
  120.      0.889615484242104552360748e+0
  121. };
  122.  
  123. static double qpzero[] = {
  124.      0.227790901973046843176842e+5,
  125.      0.413704124955104166398920e+5,
  126.      0.212153505618801157304226e+5,
  127.      0.350287351382356082073561e+4,
  128.      0.157111598580808936490685e+3,
  129.      0.1e+1
  130. };
  131.  
  132. /* qzero for x in [8,inf]
  133.  * Index 6948, 18.33 digits precision
  134.  */
  135. static double pqzero[] = {
  136.     -0.892266002008000940984692e+2,
  137.     -0.185919536443429938002522e+3,
  138.     -0.111834299204827376112621e+3,
  139.     -0.223002616662141984716992e+2,
  140.     -0.124410267458356384591379e+1,
  141.     -0.8803330304868075181663e-2,
  142. };
  143.  
  144. static double qqzero[] = {
  145.      0.571050241285120619052476e+4,
  146.      0.119511315434346136469526e+5,
  147.      0.726427801692110188369134e+4,
  148.      0.148872312322837565816135e+4,
  149.      0.905937695949931258588188e+2,
  150.      0.1e+1
  151. };
  152.  
  153.  
  154. /* yzero for x in [0,8]
  155.  * Index 6245, 18.78 digits precision
  156.  */
  157. static double pyzero[] = {
  158.     -0.275028667862910958370193e+20,
  159.      0.658747327571955492599940e+20,
  160.     -0.524706558111276494129735e+19,
  161.      0.137562431639934407857134e+18,
  162.     -0.164860581718572947312208e+16,
  163.      0.102552085968639428450917e+14,
  164.     -0.343637122297904037817103e+11,
  165.      0.591521346568688965427383e+8,
  166.     -0.413703549793314855412524e+5
  167. };
  168.  
  169. static double qyzero[] = {
  170.      0.372645883898616588198998e+21,
  171.      0.419241704341083997390477e+19,
  172.      0.239288304349978185743936e+17,
  173.      0.916203803407518526248915e+14,
  174.      0.261306575504108124956848e+12,
  175.      0.579512264070072953738009e+9,
  176.      0.100170264128890626566665e+7,
  177.      0.128245277247899380417633e+4,
  178.      0.1e+1
  179. };
  180.  
  181.  
  182. /* jone for x in [0,8]
  183.  * Index 6050, 20.98 digits precision
  184.  */
  185. static double pjone[] = {
  186.      0.581199354001606143928051e+21,
  187.     -0.667210656892491629802094e+20,
  188.      0.231643358063400229793182e+19,
  189.     -0.358881756991010605074364e+17,
  190.      0.290879526383477540973760e+15,
  191.     -0.132298348033212645312547e+13,
  192.      0.341323418230170053909129e+10,
  193.     -0.469575353064299585976716e+7,
  194.      0.270112271089232341485679e+4
  195. };
  196.  
  197. static double qjone[] = {
  198.      0.116239870800321228785853e+22,
  199.      0.118577071219032099983711e+20,
  200.      0.609206139891752174610520e+17,
  201.      0.208166122130760735124018e+15,
  202.      0.524371026216764971540673e+12,
  203.      0.101386351435867398996705e+10,
  204.      0.150179359499858550592110e+7,
  205.      0.160693157348148780197092e+4,
  206.      0.1e+1
  207. };
  208.  
  209.  
  210. /* pone for x in [8,inf]
  211.  * Index 6749, 18.11 digits precision
  212.  */
  213. static double ppone[] = {
  214.      0.352246649133679798341724e+5,
  215.      0.627588452471612812690057e+5,
  216.      0.313539631109159574238670e+5,
  217.      0.498548320605943384345005e+4,
  218.      0.211152918285396238210572e+3,
  219.      0.12571716929145341558495e+1
  220. };
  221.  
  222. static double qpone[] = {
  223.      0.352246649133679798068390e+5,
  224.      0.626943469593560511888834e+5,
  225.      0.312404063819041039923016e+5,
  226.      0.493039649018108897938610e+4,
  227.      0.203077518913475932229357e+3,
  228.      0.1e+1
  229. };
  230.  
  231. /* qone for x in [8,inf]
  232.  * Index 7149, 18.28 digits precision
  233.  */
  234. static double pqone[] = {
  235.      0.351175191430355282253332e+3,
  236.      0.721039180490447503928086e+3,
  237.      0.425987301165444238988699e+3,
  238.      0.831898957673850827325226e+2,
  239.      0.45681716295512267064405e+1,
  240.      0.3532840052740123642735e-1
  241. };
  242.  
  243. static double qqone[] = {
  244.      0.749173741718091277145195e+4,
  245.      0.154141773392650970499848e+5,
  246.      0.915223170151699227059047e+4,
  247.      0.181118670055235135067242e+4,
  248.      0.103818758546213372877664e+3,
  249.      0.1e+1
  250. };
  251.  
  252.  
  253. /* yone for x in [0,8]
  254.  * Index 6444, 18.24 digits precision
  255.  */
  256. static double pyone[] = {
  257.     -0.292382196153296254310105e+20,
  258.      0.774852068218683964508809e+19,
  259.     -0.344104806308411444618546e+18,
  260.      0.591516076049007061849632e+16,
  261.     -0.486331694256717507482813e+14,
  262.      0.204969667374566218261980e+12,
  263.     -0.428947196885524880182182e+9,
  264.      0.355692400983052605669132e+6
  265. };
  266.  
  267. static double qyone[] = {
  268.      0.149131151130292035017408e+21,
  269.      0.181866284170613498688507e+19,
  270.      0.113163938269888452690508e+17,
  271.      0.475517358888813771309277e+14,
  272.      0.150022169915670898716637e+12,
  273.      0.371666079862193028559693e+9,
  274.      0.726914730719888456980191e+6,
  275.      0.107269614377892552332213e+4,
  276.      0.1e+1
  277. };
  278.  
  279. #else
  280.  
  281. #define PI_ON_FOUR       0.78539816339744830961566084581987572
  282. #define PI_ON_TWO        1.57079632679489661923131269163975144
  283. #define THREE_PI_ON_FOUR 2.35619449019234492884698253745962716
  284. #define TWO_ON_PI        0.63661977236758134307553505349005744
  285.  
  286. static double dzero = 0.0;
  287.  
  288. /* jzero for x in [0,8]
  289.  * Index 5849, 19.22 digits precision
  290.  */
  291. static double pjzero[] = {
  292.      0.4933787251794133561816813446e+21,
  293.     -0.11791576291076105360384408e+21,
  294.      0.6382059341072356562289432465e+19,
  295.     -0.1367620353088171386865416609e+18,
  296.      0.1434354939140346111664316553e+16,
  297.     -0.8085222034853793871199468171e+13,
  298.      0.2507158285536881945555156435e+11,
  299.     -0.4050412371833132706360663322e+8,
  300.      0.2685786856980014981415848441e+5
  301. };
  302.  
  303. static double qjzero[] = {
  304.     0.4933787251794133562113278438e+21,
  305.     0.5428918384092285160200195092e+19,
  306.     0.3024635616709462698627330784e+17,
  307.     0.1127756739679798507056031594e+15,
  308.     0.3123043114941213172572469442e+12,
  309.     0.669998767298223967181402866e+9,
  310.     0.1114636098462985378182402543e+7,
  311.     0.1363063652328970604442810507e+4,
  312.     0.1e+1
  313. };
  314.  
  315. /* pzero for x in [8,inf]
  316.  * Index 6548, 18.16 digits precision
  317.  */
  318. static double ppzero[] = {
  319.     0.2277909019730468430227002627e+5,
  320.     0.4134538663958076579678016384e+5,
  321.     0.2117052338086494432193395727e+5,
  322.     0.348064864432492703474453111e+4,
  323.     0.15376201909008354295771715e+3,
  324.     0.889615484242104552360748e+0
  325. };
  326.  
  327. static double qpzero[] = {
  328.     0.2277909019730468431768423768e+5,
  329.     0.4137041249551041663989198384e+5,
  330.     0.2121535056188011573042256764e+5,
  331.     0.350287351382356082073561423e+4,
  332.     0.15711159858080893649068482e+3,
  333.     0.1e+1
  334. };
  335.  
  336. /* qzero for x in [8,inf]
  337.  * Index 6948, 18.33 digits precision
  338.  */
  339. static double pqzero[] = {
  340.     -0.8922660020080009409846916e+2,
  341.     -0.18591953644342993800252169e+3,
  342.     -0.11183429920482737611262123e+3,
  343.     -0.2230026166621419847169915e+2,
  344.     -0.124410267458356384591379e+1,
  345.     -0.8803330304868075181663e-2,
  346. };
  347.  
  348. static double qqzero[] = {
  349.     0.571050241285120619052476459e+4,
  350.     0.1195113154343461364695265329e+5,
  351.     0.726427801692110188369134506e+4,
  352.     0.148872312322837565816134698e+4,
  353.     0.9059376959499312585881878e+2,
  354.     0.1e+1
  355. };
  356.  
  357.  
  358. /* yzero for x in [0,8]
  359.  * Index 6245, 18.78 digits precision
  360.  */
  361. static double pyzero[] = {
  362.     -0.2750286678629109583701933175e+20,
  363.      0.6587473275719554925999402049e+20,
  364.     -0.5247065581112764941297350814e+19,
  365.      0.1375624316399344078571335453e+18,
  366.     -0.1648605817185729473122082537e+16,
  367.      0.1025520859686394284509167421e+14,
  368.     -0.3436371222979040378171030138e+11,
  369.      0.5915213465686889654273830069e+8,
  370.     -0.4137035497933148554125235152e+5
  371. };
  372.  
  373. static double qyzero[] = {
  374.     0.3726458838986165881989980739e+21,
  375.     0.4192417043410839973904769661e+19,
  376.     0.2392883043499781857439356652e+17,
  377.     0.9162038034075185262489147968e+14,
  378.     0.2613065755041081249568482092e+12,
  379.     0.5795122640700729537380087915e+9,
  380.     0.1001702641288906265666651753e+7,
  381.     0.1282452772478993804176329391e+4,
  382.     0.1e+1
  383. };
  384.  
  385.  
  386. /* jone for x in [0,8]
  387.  * Index 6050, 20.98 digits precision
  388.  */
  389. static double pjone[] = {
  390.      0.581199354001606143928050809e+21,
  391.     -0.6672106568924916298020941484e+20,
  392.      0.2316433580634002297931815435e+19,
  393.     -0.3588817569910106050743641413e+17,
  394.      0.2908795263834775409737601689e+15,
  395.     -0.1322983480332126453125473247e+13,
  396.      0.3413234182301700539091292655e+10,
  397.     -0.4695753530642995859767162166e+7,
  398.      0.270112271089232341485679099e+4
  399. };
  400.  
  401. static double qjone[] = {
  402.     0.11623987080032122878585294e+22,
  403.     0.1185770712190320999837113348e+20,
  404.     0.6092061398917521746105196863e+17,
  405.     0.2081661221307607351240184229e+15,
  406.     0.5243710262167649715406728642e+12,
  407.     0.1013863514358673989967045588e+10,
  408.     0.1501793594998585505921097578e+7,
  409.     0.1606931573481487801970916749e+4,
  410.     0.1e+1
  411. };
  412.  
  413.  
  414. /* pone for x in [8,inf]
  415.  * Index 6749, 18.11 digits precision
  416.  */
  417. static double ppone[] = {
  418.     0.352246649133679798341724373e+5,
  419.     0.62758845247161281269005675e+5,
  420.     0.313539631109159574238669888e+5,
  421.     0.49854832060594338434500455e+4,
  422.     0.2111529182853962382105718e+3,
  423.     0.12571716929145341558495e+1
  424. };
  425.  
  426. static double qpone[] = {
  427.     0.352246649133679798068390431e+5,
  428.     0.626943469593560511888833731e+5,
  429.     0.312404063819041039923015703e+5,
  430.     0.4930396490181088979386097e+4,
  431.     0.2030775189134759322293574e+3,
  432.     0.1e+1
  433. };
  434.  
  435. /* qone for x in [8,inf]
  436.  * Index 7149, 18.28 digits precision
  437.  */
  438. static double pqone[] = {
  439.     0.3511751914303552822533318e+3,
  440.     0.7210391804904475039280863e+3,
  441.     0.4259873011654442389886993e+3,
  442.     0.831898957673850827325226e+2,
  443.     0.45681716295512267064405e+1,
  444.     0.3532840052740123642735e-1
  445. };
  446.  
  447. static double qqone[] = {
  448.     0.74917374171809127714519505e+4,
  449.     0.154141773392650970499848051e+5,
  450.     0.91522317015169922705904727e+4,
  451.     0.18111867005523513506724158e+4,
  452.     0.1038187585462133728776636e+3,
  453.     0.1e+1
  454. };
  455.  
  456.  
  457. /* yone for x in [0,8]
  458.  * Index 6444, 18.24 digits precision
  459.  */
  460. static double pyone[] = {
  461.     -0.2923821961532962543101048748e+20,
  462.      0.7748520682186839645088094202e+19,
  463.     -0.3441048063084114446185461344e+18,
  464.      0.5915160760490070618496315281e+16,
  465.     -0.4863316942567175074828129117e+14,
  466.      0.2049696673745662182619800495e+12,
  467.     -0.4289471968855248801821819588e+9,
  468.      0.3556924009830526056691325215e+6
  469. };
  470.  
  471. static double qyone[] = {
  472.     0.1491311511302920350174081355e+21,
  473.     0.1818662841706134986885065935e+19,
  474.     0.113163938269888452690508283e+17,
  475.     0.4755173588888137713092774006e+14,
  476.     0.1500221699156708987166369115e+12,
  477.     0.3716660798621930285596927703e+9,
  478.     0.726914730719888456980191315e+6,
  479.     0.10726961437789255233221267e+4,
  480.     0.1e+1
  481. };
  482.  
  483. #endif /* ATARI && __PUREC__  ||  MTOS && __PUREC__*/
  484.  
  485. void f_real()
  486. {
  487. struct value a;
  488.     push( Gcomplex(&a,real(pop(&a)), 0.0) );
  489. }
  490.  
  491. void f_imag()
  492. {
  493. struct value a;
  494.     push( Gcomplex(&a,imag(pop(&a)), 0.0) );
  495. }
  496.  
  497. void f_arg()
  498. {
  499. struct value a;
  500.     push( Gcomplex(&a,angle(pop(&a)), 0.0) );
  501. }
  502.  
  503. void f_conjg()
  504. {
  505. struct value a;
  506.     (void) pop(&a);
  507.     push( Gcomplex(&a,real(&a),-imag(&a) ));
  508. }
  509.  
  510. /* ang2rad is 1 if we are in radians, or pi/180 if we are in degrees */
  511.  
  512. void f_sin()
  513. {
  514. struct value a;
  515.     (void) pop(&a);
  516.     push( Gcomplex(&a,sin(ang2rad*real(&a))*cosh(ang2rad*imag(&a)), cos(ang2rad*real(&a))*sinh(ang2rad*imag(&a))) );
  517. }
  518.  
  519. void f_cos()
  520. {
  521. struct value a;
  522.     (void) pop(&a);
  523.     push( Gcomplex(&a,cos(ang2rad*real(&a))*cosh(ang2rad*imag(&a)), -sin(ang2rad*real(&a))*sinh(ang2rad*imag(&a))));
  524. }
  525.  
  526. void f_tan()
  527. {
  528. struct value a;
  529. register double den;
  530.     (void) pop(&a);
  531.     if (imag(&a) == 0.0)
  532.         push( Gcomplex(&a,tan(ang2rad*real(&a)),0.0) );
  533.     else {
  534.         den = cos(2*ang2rad*real(&a))+cosh(2*ang2rad*imag(&a));
  535.         if (den == 0.0) {
  536.             undefined = TRUE;
  537.             push( &a );
  538.         }
  539.         else
  540.             push( Gcomplex(&a,sin(2*ang2rad*real(&a))/den, sinh(2*ang2rad*imag(&a))/den) );
  541.     }
  542. }
  543.  
  544. void f_asin()
  545. {
  546. struct value a;
  547. register double alpha, beta, x, y;
  548.     (void) pop(&a);
  549.     x = real(&a); y = imag(&a);
  550.     if (y == 0.0 && fabs(x) <= 1.0) {
  551.         push( Gcomplex(&a,asin(x)/ang2rad,0.0) );
  552.     } else {
  553.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  554.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  555.         push( Gcomplex(&a,asin(beta)/ang2rad, log(alpha + sqrt(alpha*alpha-1))/ang2rad) );
  556.     }
  557. }
  558.  
  559. void f_acos()
  560. {
  561. struct value a;
  562. register double alpha, beta, x, y;
  563.     (void) pop(&a);
  564.     x = real(&a); y = imag(&a);
  565.     if (y == 0.0 && fabs(x) <= 1.0) {
  566.         push( Gcomplex(&a,acos(x)/ang2rad,0.0) );
  567.     } else {
  568.         alpha = sqrt((x + 1)*(x + 1) + y*y)/2 + sqrt((x - 1)*(x - 1) + y*y)/2;
  569.         beta  = sqrt((x + 1)*(x + 1) + y*y)/2 - sqrt((x - 1)*(x - 1) + y*y)/2;
  570.         push( Gcomplex(&a,acos(beta)/ang2rad, log(alpha + sqrt(alpha*alpha-1))/ang2rad));
  571.     }
  572. }
  573.  
  574. void f_atan()
  575. {
  576. struct value a;
  577. register double x, y, u, v, w, z;
  578.     (void) pop(&a);
  579.     x = real(&a); y = imag(&a);
  580.     if (y == 0.0)
  581.         push( Gcomplex(&a,atan(x)/ang2rad, 0.0) );
  582.     else if (x == 0.0 && fabs(y) == 1.0) {
  583.         undefined = TRUE;
  584.         push(Gcomplex(&a,0.0, 0.0));
  585.     } else {
  586.             if (x >= 0) {
  587.                 u = x;
  588.             v = y;
  589.         } else {
  590.                 u = -x;
  591.             v = -y;
  592.         }
  593.         
  594.             z = atan(2*u/(1-u*u-v*v));
  595.         w = log((u*u+(v+1)*(v+1))/(u*u+(v-1)*(v-1)))/4;
  596.         if (z < 0)
  597.                 z = z + 2*PI_ON_TWO;
  598.         if (x < 0) {
  599.                 z = -z;
  600.             w = -w;
  601.         }
  602.         push( Gcomplex(&a,0.5*z/ang2rad, w) );
  603.     }
  604. }
  605.  
  606. void f_sinh()
  607. {
  608. struct value a;
  609.     (void) pop(&a);
  610.     push( Gcomplex(&a,sinh(real(&a))*cos(imag(&a)), cosh(real(&a))*sin(imag(&a))) );
  611. }
  612.  
  613. void f_cosh()
  614. {
  615. struct value a;
  616.     (void) pop(&a);
  617.     push( Gcomplex(&a,cosh(real(&a))*cos(imag(&a)), sinh(real(&a))*sin(imag(&a))) );
  618. }
  619.  
  620. void f_tanh()
  621. {
  622. struct value a;
  623. register double den;
  624.     (void) pop(&a);
  625.     den = cosh(2*real(&a)) + cos(2*imag(&a));
  626.     push( Gcomplex(&a,sinh(2*real(&a))/den, sin(2*imag(&a))/den) );
  627. }
  628.  
  629. void f_int()
  630. {
  631. struct value a;
  632.     push( Ginteger(&a,(int)real(pop(&a))) );
  633. }
  634.  
  635.  
  636. void f_abs()
  637. {
  638. struct value a;
  639.     (void) pop(&a);
  640.     switch (a.type) {
  641.         case INTGR:
  642.             push( Ginteger(&a,abs(a.v.int_val)) );            
  643.             break;
  644.         case CMPLX:
  645.             push( Gcomplex(&a,magnitude(&a), 0.0) );
  646.     }
  647. }
  648.  
  649. void f_sgn()
  650. {
  651. struct value a;
  652.     (void) pop(&a);
  653.     switch(a.type) {
  654.         case INTGR:
  655.             push( Ginteger(&a,(a.v.int_val > 0) ? 1 : 
  656.                     (a.v.int_val < 0) ? -1 : 0) );
  657.             break;
  658.         case CMPLX:
  659.             push( Ginteger(&a,(a.v.cmplx_val.real > 0.0) ? 1 : 
  660.                     (a.v.cmplx_val.real < 0.0) ? -1 : 0) );
  661.             break;
  662.     }
  663. }
  664.  
  665.  
  666. void f_sqrt()
  667. {
  668. struct value a;
  669. register double mag, ang;
  670.     (void) pop(&a);
  671.     mag = sqrt(magnitude(&a));
  672.     if (imag(&a) == 0.0 && real(&a) < 0.0)
  673.         push( Gcomplex(&a,0.0,mag) );
  674.     else
  675.     {
  676.         if ( (ang = angle(&a)) < 0.0)
  677.             ang += 2*Pi;
  678.         ang /= 2;
  679.         push( Gcomplex(&a,mag*cos(ang), mag*sin(ang)) );
  680.     }
  681. }
  682.  
  683.  
  684. void f_exp()
  685. {
  686. struct value a;
  687. register double mag, ang;
  688.     (void) pop(&a);
  689.     mag = gp_exp(real(&a));
  690.     ang = imag(&a);
  691.     push( Gcomplex(&a,mag*cos(ang), mag*sin(ang)) );
  692. }
  693.  
  694.  
  695. void f_log10()
  696. {
  697. struct value a;
  698. register double l10;;
  699.     (void) pop(&a);
  700.     l10 = log(10.0);    /***** replace with a constant! ******/
  701.     push( Gcomplex(&a,log(magnitude(&a))/l10, angle(&a)/l10) );
  702. }
  703.  
  704.  
  705. void f_log()
  706. {
  707. struct value a;
  708.     (void) pop(&a);
  709.     push( Gcomplex(&a,log(magnitude(&a)), angle(&a)) );
  710. }
  711.  
  712.  
  713. void f_floor()
  714. {
  715. struct value a;
  716.  
  717.     (void) pop(&a);
  718.     switch (a.type) {
  719.         case INTGR:
  720.             push( Ginteger(&a,(int)floor((double)a.v.int_val)));            
  721.             break;
  722.         case CMPLX:
  723.             push( Ginteger(&a,(int)floor(a.v.cmplx_val.real)));
  724.     }
  725. }
  726.  
  727.  
  728. void f_ceil()
  729. {
  730. struct value a;
  731.  
  732.     (void) pop(&a);
  733.     switch (a.type) {
  734.         case INTGR:
  735.             push( Ginteger(&a,(int)ceil((double)a.v.int_val)));            
  736.             break;
  737.         case CMPLX:
  738.             push( Ginteger(&a,(int)ceil(a.v.cmplx_val.real)));
  739.     }
  740. }
  741.  
  742. /* bessel function approximations */
  743. static double jzero(x)
  744. double x;
  745. {
  746. double p, q, x2;
  747. int n;
  748.  
  749.     x2 = x * x;
  750.     p = pjzero[8];
  751.     q = qjzero[8];
  752.     for (n=7; n>=0; n--) {
  753.         p = p*x2 + pjzero[n];
  754.         q = q*x2 + qjzero[n];
  755.     }
  756.     return(p/q);
  757. }
  758.  
  759. static double pzero(x)
  760. double x;
  761. {
  762. double p, q, z, z2;
  763. int n;
  764.  
  765.     z = 8.0 / x;
  766.     z2 = z * z;
  767.     p = ppzero[5];
  768.     q = qpzero[5];
  769.     for (n=4; n>=0; n--) {
  770.         p = p*z2 + ppzero[n];
  771.         q = q*z2 + qpzero[n];
  772.     }
  773.     return(p/q);
  774. }
  775.  
  776. static double qzero(x)
  777. double x;
  778. {
  779. double p, q, z, z2;
  780. int n;
  781.  
  782.     z = 8.0 / x;
  783.     z2 = z * z;
  784.     p = pqzero[5];
  785.     q = qqzero[5];
  786.     for (n=4; n>=0; n--) {
  787.         p = p*z2 + pqzero[n];
  788.         q = q*z2 + qqzero[n];
  789.     }
  790.     return(p/q);
  791. }
  792.  
  793. static double yzero(x)
  794. double x;
  795. {
  796. double p, q, x2;
  797. int n;
  798.  
  799.     x2 = x * x;
  800.     p = pyzero[8];
  801.     q = qyzero[8];
  802.     for (n=7; n>=0; n--) {
  803.         p = p*x2 + pyzero[n];
  804.         q = q*x2 + qyzero[n];
  805.     }
  806.     return(p/q);
  807. }
  808.  
  809. static double rj0(x)
  810. double x;
  811. {
  812.     if ( x <= 0.0 )
  813.         x = -x;
  814.     if ( x < 8.0 )
  815.         return(jzero(x));
  816.     else
  817.         return( sqrt(TWO_ON_PI/x) *
  818.             (pzero(x)*cos(x-PI_ON_FOUR) - 8.0/x*qzero(x)*sin(x-PI_ON_FOUR)) );
  819.  
  820. }
  821.  
  822. static double ry0(x)
  823. double x;
  824. {
  825.     if ( x < 0.0 )
  826.         return(dzero/dzero); /* error */
  827.     if ( x < 8.0 )
  828.         return( yzero(x) + TWO_ON_PI*rj0(x)*log(x) );
  829.     else
  830.         return( sqrt(TWO_ON_PI/x) *
  831.             (pzero(x)*sin(x-PI_ON_FOUR) + 
  832.             (8.0/x)*qzero(x)*cos(x-PI_ON_FOUR)) );
  833.  
  834. }
  835.  
  836.  
  837. static double jone(x)
  838. double x;
  839. {
  840. double p, q, x2;
  841. int n;
  842.  
  843.     x2 = x * x;
  844.     p = pjone[8];
  845.     q = qjone[8];
  846.     for (n=7; n>=0; n--) {
  847.         p = p*x2 + pjone[n];
  848.         q = q*x2 + qjone[n];
  849.     }
  850.     return(p/q);
  851. }
  852.  
  853. static double pone(x)
  854. double x;
  855. {
  856. double p, q, z, z2;
  857. int n;
  858.  
  859.     z = 8.0 / x;
  860.     z2 = z * z;
  861.     p = ppone[5];
  862.     q = qpone[5];
  863.     for (n=4; n>=0; n--) {
  864.         p = p*z2 + ppone[n];
  865.         q = q*z2 + qpone[n];
  866.     }
  867.     return(p/q);
  868. }
  869.  
  870. static double qone(x)
  871. double x;
  872. {
  873. double p, q, z, z2;
  874. int n;
  875.  
  876.     z = 8.0 / x;
  877.     z2 = z * z;
  878.     p = pqone[5];
  879.     q = qqone[5];
  880.     for (n=4; n>=0; n--) {
  881.         p = p*z2 + pqone[n];
  882.         q = q*z2 + qqone[n];
  883.     }
  884.     return(p/q);
  885. }
  886.  
  887. static double yone(x)
  888. double x;
  889. {
  890. double p, q, x2;
  891. int n;
  892.  
  893.     x2 = x * x;
  894.     p = 0.0;
  895.     q = qyone[8];
  896.     for (n=7; n>=0; n--) {
  897.         p = p*x2 + pyone[n];
  898.         q = q*x2 + qyone[n];
  899.     }
  900.     return(p/q);
  901. }
  902.  
  903. static double rj1(x)
  904. double x;
  905. {
  906. double v,w;
  907.     v = x;
  908.     if ( x < 0.0 )
  909.         x = -x;
  910.     if ( x < 8.0 )
  911.         return(v*jone(x));
  912.     else {
  913.         w = sqrt(TWO_ON_PI/x) *
  914.             (pone(x)*cos(x-THREE_PI_ON_FOUR) - 
  915.                8.0/x*qone(x)*sin(x-THREE_PI_ON_FOUR)) ;
  916.         if (v < 0.0)
  917.             w = -w;
  918.         return( w );
  919.     }
  920. }
  921.  
  922. static double ry1(x)
  923. double x;
  924. {
  925.     if ( x <= 0.0 )
  926.         return(dzero/dzero); /* error */
  927.     if ( x < 8.0 )
  928.         return( x*yone(x) + TWO_ON_PI*(rj1(x)*log(x) - 1.0/x) );
  929.     else
  930.         return( sqrt(TWO_ON_PI/x) *
  931.             (pone(x)*sin(x-THREE_PI_ON_FOUR) + 
  932.             (8.0/x)*qone(x)*cos(x-THREE_PI_ON_FOUR)) );
  933. }
  934.  
  935.  
  936. void f_besj0()    
  937. {
  938. struct value a;
  939.     (void) pop(&a);
  940.     if (fabs(imag(&a)) > zero)
  941.         int_error("can only do bessel functions of reals",NO_CARET);
  942.     push( Gcomplex(&a,rj0(real(&a)),0.0) );
  943. }
  944.  
  945.  
  946. void f_besj1()    
  947. {
  948. struct value a;
  949.     (void) pop(&a);
  950.     if (fabs(imag(&a)) > zero)
  951.         int_error("can only do bessel functions of reals",NO_CARET);
  952.     push( Gcomplex(&a,rj1(real(&a)),0.0) );
  953. }
  954.  
  955.  
  956. void f_besy0()    
  957. {
  958. struct value a;
  959.     (void) pop(&a);
  960.     if (fabs(imag(&a)) > zero)
  961.         int_error("can only do bessel functions of reals",NO_CARET);
  962.     if (real(&a) > 0.0)
  963.         push( Gcomplex(&a,ry0(real(&a)),0.0) );
  964.     else {
  965.         push( Gcomplex(&a,0.0,0.0) );
  966.         undefined = TRUE ;
  967.     }
  968. }
  969.  
  970.  
  971. void f_besy1()    
  972. {
  973. struct value a;
  974.     (void) pop(&a);
  975.     if (fabs(imag(&a)) > zero)
  976.         int_error("can only do bessel functions of reals",NO_CARET);
  977.     if (real(&a) > 0.0)
  978.         push( Gcomplex(&a,ry1(real(&a)),0.0) );
  979.     else {
  980.         push( Gcomplex(&a,0.0,0.0) );
  981.         undefined = TRUE ;
  982.     }
  983. }
  984.